package com.api.gbhg.liuwenbo.api;


import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.pojo.Info;
import com.api.gbhg.liuwenbo.utils.FormatUtil;
import com.api.gbhg.liuwenbo.utils.Utils;
import weaver.conn.RecordSet;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

/**
 * 非中央财政拨款实际收支情况
 */
@Path("/gb/NonCentral")
public class IncomeExpendtureApi {

    private String localFinance = "uf_dfczbkxm"; //地方财政
    private String noCentralIncome = "uf_fzyczbkjcsj"; //非中央

    //6-基本支出、8-中央转移支付、9-横向拨款、10-利息收入、3-事业收入、4-事业单位经营收入、5-下级单位上缴收入、2-上级补助收入、11-用事业基金弥补收支差额
    int[] arr = {6,8,9,10,3,4,5,2,11};

    @POST
    @Path("/InandOutDetails")
    @Produces(MediaType.APPLICATION_JSON)
    public String InandOutDetails(Map<String,Object> param){
        Integer dw = (Integer) param.get("dw");
        Integer nf = LocalDate.now().getYear();
        if(param.containsKey("nf"))
            nf = (Integer) param.get("nf");

        Info[] details = getDetails(dw,nf);
        return JSON.toJSONString(details);
    }

    /**
     * 获取详情数据
     * @param dw
     * @return
     */
    public Info[] getDetails(Integer dw,Integer nf){
        Map<String, Object>[] projectOut = getProjectDa(dw,nf);
        Info[] infos = new Info[10];
        infos[0] = new Info();
        infos[0].setData(projectOut);
        infos[0].setTitle("项目支出");
        infos[0].setId("xmzc");
        for (int i = 0; i < arr.length; i++) {
            infos[i+1] = new Info();

            Map<String, Object>[] datas = getDatas(dw, arr[i],nf);
            infos[i+1].setData(datas);

            String title = getBudgetSources(arr[i]);
            infos[i+1].setTitle(title);
            infos[i+1].setId(String.valueOf(arr[i]));
        }
        return Utils.removeExcessLenInfo(infos);
    }

    public String getBudgetSources(Integer val){
        RecordSet rs = new RecordSet();
        String sql = "select yslymc from uf_budget_sources where id =  "+val+" ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("yslymc");
    }

    /**
     * 项目
     * @param dw
     * @return
     */
    public Map<String, Object>[] getProjectDa(Integer dw,Integer nf){
        RecordSet rs = new RecordSet();
        String sql;
        if (dw == 0){
            sql = "select * from "+localFinance+" where  nf = "+nf+" ";
        }else{
            sql = "select * from "+localFinance+" where dw = "+dw+" and nf = "+nf+" ";
        }
        rs.executeQuery(sql);
        Map<String,Object> map[] = new HashMap[rs.getCounts()];
        int c = 0;
        while (rs.next()){
            map[c] = new HashMap<>();
            String xmmc = rs.getString("xmmc");

            Map<String, Object> lastyearDa = getProjectLastyear(dw, xmmc); //上年数据
            if (!lastyearDa.isEmpty()) {
                map[c].putAll(lastyearDa);
            }else{
                map[c].put("snsjsrwy", FormatUtil.DmFormat(0));
                map[c].put("snjzjewy", FormatUtil.DmFormat(0));
            }

            map[c].put("xmmc",xmmc);
            map[c].put("yjsrwy",FormatUtil.DmFormat(rs.getDouble("yjsrwy")));
            map[c].put("sjsrwy",FormatUtil.DmFormat(rs.getDouble("sjsrwy")));
            map[c].put("sjzcwy",FormatUtil.DmFormat(rs.getDouble("sjzcwy")));
            map[c].put("jzjewy",FormatUtil.DmFormat(rs.getDouble("jzjewy")));
            c++;
        }
        return map;
    }

    /**
     * 上年项目
     * @param dw
     * @param name
     * @return
     */
    public Map<String,Object> getProjectLastyear(Integer dw,String name){
        RecordSet rs = new RecordSet();
        int year = (LocalDate.now().getYear())-1; //上年
        String sql;
        if (dw == 0){
            sql = " select sum(sjsrwy) snsjsrwy,sum(jzjewy) snjzjewy from uf_dfczbkxm where nf = "+year+" and xmmc = '"+name+"' group by xmmc   ";
        }else{
            sql = " select sum(sjsrwy) snsjsrwy,sum(jzjewy) snjzjewy from uf_dfczbkxm where dw = "+dw+" and nf = "+year+" and xmmc = '"+name+"' group by xmmc   ";
        }
        rs.executeQuery(sql);
        Map<String,Object> map = new HashMap<>();
        while (rs.next()){
            map.put("snsjsrwy", FormatUtil.DmFormat(rs.getDouble("snsjsrwy")));
            map.put("snjzjewy", FormatUtil.DmFormat(rs.getDouble("snjzjewy")));
        }
        return map;
    }


    /**
     * 其他
     * @param dw
     * @param ly
     * @return
     */
    public Map<String,Object>[] getDatas(Integer dw,Integer ly,Integer nf){
        RecordSet rs = new RecordSet();
        String sql;
        if (dw == 0){
            sql = "select yjsrwy,sjsrwy,sjzcwy,jzjewy from uf_fzyczbkjcsj where srly = "+ly+" and srnf = "+nf+"  ";
        }else{
            sql = "select yjsrwy,sjsrwy,sjzcwy,jzjewy from uf_fzyczbkjcsj where srdw = "+dw+" and srly = "+ly+" and srnf = "+nf+" ";
        }
        rs.executeQuery(sql);
        Map<String,Object>[] map = new HashMap[rs.getCounts()];
        int i = 0;
        while (rs.next()){
            map[i] = new HashMap<>();

            Map<String, Object> lastyearMap = getLastyearDatas(dw, ly);
            if (!lastyearMap.isEmpty()) {
                map[i].putAll(lastyearMap);
            }else{
                map[i].put("snsjsrwy",FormatUtil.DmFormat(0));
                map[i].put("snjzjewy",FormatUtil.DmFormat(0));
            }

            map[i].put("yjsrwy",FormatUtil.DmFormat(rs.getDouble("yjsrwy")));
            map[i].put("sjsrwy",FormatUtil.DmFormat(rs.getDouble("sjsrwy")));
            map[i].put("sjzcwy",FormatUtil.DmFormat(rs.getDouble("sjzcwy")));
            map[i].put("jzjewy",FormatUtil.DmFormat(rs.getDouble("jzjewy")));
            i++;
        }
        return map;
    }

    /**
     * 上年数据
     * @param dw
     * @param ly
     * @return
     */
    public Map<String,Object> getLastyearDatas(Integer dw,Integer ly){
        RecordSet rs = new RecordSet();
        int year = (LocalDate.now().getYear())-1; //上年
        String sql ;
        if (dw == 0){
            sql = "select sum(sjsrwy) snsjsrwy,sum(jzjewy) snjzjewy from uf_fzyczbkjcsj where srly = "+ly+" and srnf = "+year+" ";
        }else{
            sql = "select sum(sjsrwy) snsjsrwy,sum(jzjewy) snjzjewy from uf_fzyczbkjcsj where srdw = "+dw+" and srly = "+ly+" and srnf = "+year+" ";
        }
        rs.executeQuery(sql);
        Map<String,Object> map = new HashMap<>();
        while (rs.next()){
            map.put("snsjsrwy",FormatUtil.DmFormat(rs.getDouble("snsjsrwy")));
            map.put("snjzjewy",FormatUtil.DmFormat(rs.getDouble("snjzjewy")));
        }
        return map;
    }
}
